<?php

/* 
 * Controller/SyntheseController.php
 */
namespace RHX\Controller;

use RHX\Model\Manager\SyntheseManager;
use Zend\View\Model\ViewModel;
use RHX\Model\Object\InstructionPaie;

class SyntheseController extends RHXController{
    /**
     *
     * @var SyntheseManager
     */
    protected $manager = null;
    private $postDefined = false;
    
    protected $site = null;
    protected $periode = null;
    
    /**
     * Page d'accueil
     * 
     * @return \Zend\View\Model\ViewModel
     */
    public function indexAction(){
	$this->thePost();
	$post = $this->getPost();
	$index = new ViewModel();
	$loadIndex = $this->loadIndexAction();
	$index->addChild($loadIndex, 'loadIndex');
	$index->setTemplate('rhx/synthese/index.phtml');
	$index->setVariables(array(
	    'site' => $this->getManager()->getSite($post->site),
	    'periode' => $this->getManager()->getPeriode($post->periode),
	));
	$this->layout('layout/rhx2');
	return $index;
    }
    
    /**
     * Contenu de la page
     * 
     * @return \Zend\View\Model\ViewModel
     */
    public function loadIndexAction(){
	$post = $this->getPost();
	$data = $this->getManager()->loadIndex($post);
	$loadIndex = new ViewModel(array(
	    'syntheses' => $data,
	    'periode' => $post->periode,
	    'site' => $post->site,
		));
	$loadIndex->setTemplate('rhx/synthese/load-index.phtml');
	$this->layout('layout/ajax');
	return $loadIndex;
    }
    
    /**
     * Synthese Manager
     * 
     * @return SyntheseManager
     */
    public function getManager(){
	if(is_null($this->manager)){
	    $this->manager = new SyntheseManager($this->getAdapter());
	}
	return $this->manager;
    }
    
    /**
     * Traitement et manipulation des données en POST
     */
    private function thePost(){
	if($this->postDefined) {return;}
	// remplissage du POST
	$post = $this->getRequest()
		->getPost();
	$post->set('action', InstructionPaie::CREATE);

	// paramètres
	$siteId		 = (int) $this->params()
			->fromRoute('id', -1);
	$periodeId	 = (int) $this->params()
			->fromRoute('other1', -1);
	// object
	$site		 = null;
	if ($siteId < 0) {
	    $site = $this->navigation
		    ->offsetGet('site');
	    $post->set('site', $site->id);
	}
	else {
	    $tableSite = new \RHX\Model\Table\SiteTable($this->getAdapter());
	    try {
		$site = $tableSite->get($siteId);
		$this->changeSite($siteId);
		$post->set('site', $siteId);
	    }
	    catch (\Exception $ex) {
		$site = $tableSite->getAll()
			->current();
		$this->changeSite($site->id);
		$post->set('site', $site->id);
	    }
	}
	$periode = null;
	if ($periodeId < 0) {
	    $periode = $this->navigation
		    ->offsetGet('periode');
	    $post->set('periode', $periode->id);
	}
	else {
	    $tablePeriode = new \RHX\Model\Table\PeriodeTable($this->getAdapter());
	    try {
		$periode = $tablePeriode->get($periodeId);
		$this->changePeriode($periodeId);
		$post->set('periode', $periodeId);
	    }
	    catch (\Exception $ex) {
		$periode = $tableSite->getAll()
			->current();
		$this->changePeriode($periode->id);
		$post->set('periode', $periode->id);
	    }
	}
	$this->postDefined = true;
	$this->site = $site;
	$this->periode = $periode;
    }
    
    /**
     * Liste des employés pour la Synthèse de paie
     * 
     * @return \Zend\View\Model\JsonModel
     */
    public function siteListAction(){
	$data = $this->getManager()
		->getSiteList(array('Total COJEAN'));
	return new \Zend\View\Model\JsonModel($data);
    }
    
    public function loadTotalAction(){
	$this->layout('layout/ajax');
	$post = $this->getPost();
	$total = $this->getManager()->getTotal($post);
	$total->setTemplate('rhx/synthese/load-index.phtml');
	return $total;
    }
}

